

//发货单管理详情（整车）
//杨帆

interface IOrderDeliverManagentDetailViewModel extends ng.IScope {// extends ng.IScope
    /**
     * 订单管理详情
     */
    orderData: invoiceManagement;
    /**
     * 合同信息
     */
    contractData: invoiceManagementContract;
    /**
     * 订单总单
     */
    OData: invoiceManagementOrder;
    /**
     * 总单列表
     */
    orderTotalData: Array<invoiceManagementOrder>;
    /**
     * 子单列表
     */
    orderChildData: Array<invoiceManagementOrderChild>;
    /**
     * 费用详情列表
     */
    feeCostList: Array<invoiceManagementFee>;//feeListResponse
    /**
     * 查看费用详情事件
     */
    costDetial: (item: invoiceManagementFee) => void;
    costDate: invoiceManagementFee;
    /**
      * app竞价
      */
    orderAppBiddingList: Array<quotationResponse>;
    /**
     * 应收
     */
    orderReceivalbleList: Array<receivableListResponse>;
    /**
     * 退回派车
     */
    backCar: () => void;
    /**
     * 隐藏/显示 退回派车按钮 
     */
    backShow: boolean;
    /**
     * 返回事件
     */
    backUrl: () => void;
    /**
     * 订单详情事件
     */
    getOrderDetial: (item: invoiceManagementOrder) => void;
    /**
     * 子单详情事件
     */
    editChildren: (item: invoiceManagementOrderChild) => void;
    /**
     * 应收信息事件
     */
    editReceivable: (item: invoiceManagementReceivable) => void;
    /**
     * 订单查看详情数据
     */
    orderDetail: invoiceManagementOrder;
    /**
     * 子单查看详情
     */
    orderChildDetail: invoiceManagementOrderChild;
    /**
     * 应收查看详情
     */
    orderReceivalbleDetail: invoiceManagementReceivable;
    /**
     * 查看详情事件
     */
    showContractDetail: () => void;
    /**
    * 是否显示电子合同
    */
    isContractO: boolean;
    /**
     * 是否显示补充协议
     */
    isAgreementO: boolean;
    /**
    * 电子合同按钮是否启用
    */
    contractT: boolean;
    /**
     * 点击显示电子合同
     */
    ContractO: () => void;
    /**
     * 点击显示补充协议
     */
    AgreementO: () => void;
    /**
    * 是否显示切换补充协议TAB
    */
    isAgreementF: boolean;
    /**
    * 电子合同第一张图
    */
    contractImgA: string;
    /**
     * 电子合同第二张图
     */
    contractImgB: string;
    /**
     * 电子合同第三张图
     */
    contractImgC: string;
    /**
     * 电子合同类型
     */
    eContractType: string;

    /**
    * 电子合同编号
    */
    electronicContractNumber: string;
    /**
     * 电子合同状态
     */
    electronicContractStatus: string;
    /**
     * 补充协议合同编号
     */
    agreementContractNumber: string;
    /**
     * 补充协议合同状态
     */
    agreementContractStatus: string;
    /**
     * 补充协议第一张图
     */
    agreementImgA: string;
    /**
     * 补充协议第二张图
     */
    agreementImgB: string;
    /**
     * 补充协议第三张图
     */
    agreementImgC: string;

    /**
     * 驾驶证上传组件
     */
    driverUploader: any;
    /**
     * 挂车证上传组件
     */
    trailerUploader: any;
    /**
     * 行驶证上传组件
     */
    travelUploader: any;
    /**
     * 身份证上传组件
     */
    idCardUploader: any;
    /**
     * 车辆照片
     */
    carPicUploader: any;

    /**
    * 道路运输证
    */
    roadTransportCardUploader: any
    /**
     * 道路运输经营许可证
     */
    roadTransportLicenseUploader: any;
    /**
     * 从业资格证
     */
    occupationalLicenseUploader: any;
    /**
     * 磅单照片
     */
    weightNoteUploader:any;
    /**
     * 订单附件
     */
    orderUploader: any;
    /**
     * 回单附件
     */
    receiptUploader: any;

    
    /**
     * 承运单id
     */
    id: string;
    /**
     * 来货拉用户组
     */
    ccpCarDetali: string;
    /**
     * 锚点
     */
    anchor: (attr: string) => void;

    toggleAnchor: () => void;




    //11.1新增
    /**
     * App竞价查看车辆信息
     */
    carOrderDeliverDetailApp: (item: quotationResponse) => void;
    /**
     * 点击查看车辆详情ID
     */
    checkDetailCarId:string;
    /**
     * App车辆详情中车辆ID
     */
    carId_: string;
    /**
     * 点击附件图片触发
     */
    handleImgClick: (any) => void;
    /**
     * 点击中用以旋转放大的图片src
     */
    selectedImgSrc: string;
    /**
     * 订单是否绑定定位器
     */
    orderBinded: boolean;
    /**
     * 通过定位器定位
     */
    locateByTool: () => void;
    /**
     * 是否显示定位器定位的modal
     */
    showLcoToolMdal: boolean;
    /**
     * 定位器定位的返回按钮
     */
    lt_back: () => void;

    attachList:OssUploaderItem[];
    
    // 回单附件列表
    receiptAttachList:OssUploaderItem[];
}





class IOrderDeliverManagentController_detail {
    /**
     * 订单状态
     */
    type: string;
    /**
     * 隐藏退回派车按钮
     */
    backShow = false;
    /**
     * 附件
     */
    uploaderList: AddCarAttachmentDetail[] = [];
    /**
     * 附件地址
     */
    baseImageUrl: string;


    constructor(private $scope: IOrderDeliverManagentDetailViewModel, private orderService: IorderService, private $location: ng.ILocationService,
        private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private routeService: routeService,
        private carService: ICarService, private $http: ng.IHttpService, private commonService: commonService, private $anchorScroll: ng.IAnchorScrollService,
        private areaService: IAreaService, private $timeout: ng.ITimeoutService, private locationService: ILocationService,private ossUploaderService:ossUploaderService) {


        this.init();
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";



    }

    init(): void {


        this.$scope.id = this.$location.search().id;
        // this.$scope.id ="111",
        this.type = this.$location.search().type;
        this.$scope.anchor = this.anchor;
        this.$scope.toggleAnchor = this.toggleAnchor;
        this.loadDataDetail();

        this.$scope.backUrl = this.backUrl;
        this.$scope.costDetial = this.costDetial;
        this.$scope.showContractDetail = this.showContractDetail;
        this.$scope.AgreementO = this.AgreementO;
        this.$scope.ContractO = this.ContractO;
        this.$scope.isContractO = true;
        this.$scope.isAgreementO = false;
        this.$scope.contractT = true;
        this.$scope.isAgreementF = false;
        //承运单附件
        const OssFile = this.ossUploaderService.getOssFile();

        this.$scope.attachList=[
            {
                title:'驾驶证',
                uploader:null,
                uploaderName:'driverUploader'
            },{
                title:'挂车证',
                uploader:null,
                uploaderName:'trailerUploader'
            },{
                title:'行驶证',
                uploader:null,
                uploaderName:'travelUploader'
            },{
                title:'身份证',
                uploader:null,
                uploaderName:'idCardUploader'
            },{
                title:'车辆照片',
                uploader:null,
                uploaderName:'carPicUploader'
            },{
                title:'道路运输证',
                uploader:null,
                uploaderName:'roadTransportCardUploader'
            },{
                title:'道路运输经营许可证',
                uploader:null,
                uploaderName:'roadTransportLicenseUploader'
            },{
                title:'从业资格证',
                uploader:null,
                uploaderName:'occupationalLicenseUploader'
            },{
                title:'磅单照片',
                uploader:null,
                uploaderName:'weightNoteUploader'
            },
        ]
        this.$scope.receiptAttachList=[];

        this.$scope.attachList.forEach((attach,index)=>{
            attach.uploader=new OssFile(this.$scope,attach.uploader,{uploaderList:[],tag:index+1});
        })

        //订单附件
        this.$scope.orderUploader = new OssFile(this.$scope,this.$scope.orderUploader,{uploaderList:this.uploaderList,tag:1,multiple:true});
        // this.$scope.orderUploader.filters.push({ name: 'imageFilter', fn: this.commonService.ownFilter });
        // this.$scope.orderUploader.onDownload = (item, tag) => { this.commonService.onDownload(item, tag, this.uploaderList) };



        //APP竞价中车辆详情

        this.$scope.carOrderDeliverDetailApp = this.carOrderDeliverDetailApp
        this.$scope.handleImgClick = (item) => {

            // $('#imgRotate').modal('show');
            this.$scope.selectedImgSrc = item.imgSrc;
        }

        this.$scope.lt_back = () => { $('#locToolLocation').modal('hide'); }
        this.$scope.locateByTool = () => { this.$scope.showLcoToolMdal = true; }
        $('#locToolLocation').on('hidden.bs.modal', (e) => {
            this.$scope.showLcoToolMdal = false;
            if (!this.$scope.$$phase) {
                this.$scope.$apply(() => { this.$scope.showLcoToolMdal });
            }
        });


    }


    /**
     * 加载数据
     */
    loadDataDetail = () => {
        this.orderService.invoiceManagementDetail(this.$scope.id).then((res) => {
            /**
             * 承运单data
             */
            this.$scope.orderData = res.data;
            this.carService.getDetail(res.data.carId).then(res => {
                this.$scope.ccpCarDetali = res.data.carCode + res.data.driver + res.data.driverPhone;
            });
            /**
             * 合同data
             */
            this.$scope.contractData = res.data.contract;
            if (this.$scope.contractData != null) {
                this.$scope.electronicContractNumber = res.data.contract.electronicContractNumber;
                if (this.$scope.electronicContractNumber != "") {
                    this.$scope.contractT = false;
                }
                if (this.$scope.electronicContractNumber == "") {//暂无电子合同按钮灰显
                    this.$scope.contractT = true;
                }
                this.$scope.agreementContractNumber = res.data.contract.agreementContractNumber;
                if (this.$scope.agreementContractNumber != "") {
                    this.$scope.isAgreementF = true;
                }
            }

            /**
             * 订单data
             */
            this.$scope.OData = res.data.orderList[0];
            this.$scope.orderBinded = false;
            /**
             * 判断订单是否已经绑定到定位器
             */
            this.locationService.checkOrderBinded(this.$scope.OData.id, !(this.$scope.OData.status === "货已送达")).then(res => {
                if (res.data.extData == true) {
                    this.$scope.orderBinded = true;
                }
            })
            /**
             * 总单列表
             */
            this.$scope.orderTotalData = res.data.orderList;
            /**
             * 子单列表
             */
            this.$scope.orderChildData = res.data.orderList[0].childList;
            /**
             * 付款信息
             */
            this.$scope.feeCostList = res.data.feeList;

            /**
             * APP竞价
             */
            this.orderService.quotation(res.data.orderList[0].id).then((res) => {
                this.$scope.orderAppBiddingList = res.data.data;
            });

            /**
            * 订单查看详情事件
            */
            this.$scope.getOrderDetial = this.getOrderDetial;
            /**
            * 子单查看详情事件
            */
            this.$scope.editChildren = this.editChildren;
            /**
            * 应收查看详情事件
            */
            this.$scope.editReceivable = this.editReceivable;
            /**
             * 付款信息事件
             */

            /**
             * 应收信息
             */
            let receivalbleArr = [];
            res.data.orderList.forEach((receivable, index) => {
                /**
                 * 订单应收
                 */
                if (receivable.receivableList.length > 0) {
                    receivable.receivableList.forEach((itemR, index) => {
                        receivalbleArr.push(itemR);
                    });
                }
                /**
                 * 子单应收
                 */
                receivable.childList.forEach((chilItem, index) => {
                    if (chilItem.receivableList.length > 0) {
                        chilItem.receivableList.forEach((itemRC, index) => {
                            receivalbleArr.push(itemRC);
                        });
                    }
                });

            });
            this.$scope.orderReceivalbleList = receivalbleArr;

            /**
             * 判断是否有“退回派车”按钮
             */
            if (this.$location.search().isReverse) {
                window.localStorage.getItem("loginData").toString().indexOf("客服经理") != -1 && res.data.orderList[0].status == "货已送达" && res.data.feeList.length < 1 ? this.$scope.backShow = true : this.$scope.backShow = false;
            } else {
                window.localStorage.getItem("loginData").toString().indexOf("客服经理") != -1 && this.type == "货已送达" && res.data.feeList.length < 1 ? this.$scope.backShow = true : this.$scope.backShow = false;
            }

            /**
             * 退回派车
             */
            this.$scope.backCar = this.backCar;

            /**
             * 承运商附件
             */
            let carArr = res.data.carrierOrderAttachments;
            carArr.forEach((item, index) => {
                this.uploaderList.push(item);
                
                this.$scope.attachList[Number(item.attachmentTag)-1].uploader.addByAttach(item)

                
            });

            /**
             * 订单附件
             */
            let orderArr = this.$scope.OData.orderAttachments;
            orderArr.forEach((itemO, index) => {
                this.uploaderList.push(itemO);
                // this.commonService.showFileItem(itemO, this.$scope.orderUploader);
                this.$scope.orderUploader.addByAttach(itemO)
            });

            /**
             * 回单附件
             */
            var childOrderAttachment = [];
            this.$scope.receiptUploader = [];
            const OssFile=this.ossUploaderService.getOssFile();
            this.$scope.orderChildData.forEach((item, index) => {

                this.$scope.receiptAttachList.push(
                    {
                        title:'回单'+(index+1),
                        uploader:null,
                        uploaderName: 'receipt' + index+'Uploader'
                    }
                )
                // 有一个回单，则生成一个
               

                this.$scope.receiptAttachList[index].uploader=new  OssFile(this.$scope,this.$scope.receiptAttachList[index].uploader,{uploaderList:[],tag:index+1,multiple:true});

             
                let receiptUploaderArr = item.receiptAttachments;
                if (receiptUploaderArr.length > 0) {
                    for (let i = 0; i < receiptUploaderArr.length; i++) {
                        let receiptUploaderS = receiptUploaderArr[i];

                        this.$scope.receiptAttachList[index].uploader.addByAttach(receiptUploaderS)
                    }
                 
                }
            });



        });
    }

    /**
     * 退回派车
     */
    backCar = () => {
        this.$ngBootbox.confirm('确定退回派车吗？').then((res) => {
            this.orderService.managerReturnDispatch(this.$scope.id).then((res) => {
                this.$state.go('app.order.orderDeliverManage', { name: 'Manage' });
            });
        });
    }
    /**
     * 返回事件
     */
    backUrl = () => {
        if (this.$location.search().name == "reverseComment") {
            this.$state.go('app.eva.evaluate_reverseManage', { name: 'reverseComment' });
            return;
        }
        this.$state.go('app.order.orderDeliverManage', { name: 'Manage' });
    }

    /**
     * 订单查看详情事件
     */
    getOrderDetial = (item: invoiceManagementOrder) => {
        this.$scope.orderDetail = item;
    }

    /**
     * 子单查看详情事件
     */
    editChildren = (item: invoiceManagementOrderChild) => {
        this.$scope.orderChildDetail = item;
    }

    /**
     * 应收查看详情事件
     */
    editReceivable = (item: invoiceManagementReceivable) => {
        this.$scope.orderReceivalbleDetail = item;
    }

    /**
     * 费用信息
     */
    costDetial = (item: invoiceManagementFee) => {
        this.$scope.costDate = item;
    }


    /**
    * 点击显示电子合同
    */
    ContractO = () => {
        if (this.$scope.electronicContractNumber != null && this.$scope.electronicContractNumber != "") {
            this.$scope.isContractO = false;
            this.orderService.EContractDetail(this.$scope.electronicContractNumber).then((res) => {
                this.$scope.contractImgA = res.data.imgList[0];
                this.$scope.contractImgB = res.data.imgList[1];
                this.$scope.contractImgC = res.data.imgList[2];
            }, (rej) => { });
        }
    }
    /**
     * 点击显示补充协议
     */
    AgreementO = () => {
        this.$scope.isAgreementO = true;
        this.$scope.isContractO = false;
        if (this.$scope.agreementContractNumber != "" && this.$scope.agreementContractNumber != null) {
            this.orderService.EContractDetail(this.$scope.agreementContractNumber).then((res) => {
                this.$scope.agreementImgA = res.data.imgList[0];
                this.$scope.agreementImgB = res.data.imgList[1];
                this.$scope.agreementImgC = res.data.imgList[2];
                // let agreementContractNumber = this.$scope.agreementContractNumber;
            }, (rej) => { });
        }

    }

    /**
   * 查看电子合同详情
   */
    showContractDetail = () => {
        if (this.$scope.electronicContractNumber != null && this.$scope.electronicContractNumber != "") {
            this.orderService.EContractDetail(this.$scope.electronicContractNumber).then((res) => {
                this.$scope.contractImgA = res.data.imgList[0];
                this.$scope.contractImgB = res.data.imgList[1];
                this.$scope.contractImgC = res.data.imgList[2];
                this.$scope.isContractO = false;
                $('#Econtract').modal('show');
            }, (rej) => { });
        }

    }

    anchor = (attr) => {
        this.$location.hash(attr);
        this.$anchorScroll.yOffset = 1000;
        this.$anchorScroll();
    }

    toggleAnchor() {
        $("#anchorCon").toggleClass('hide');
        $("#anchor").toggleClass('anchor-compaction')
    }



    //11.1新增

    /**
     * 获取 App竞价 车辆详情
     */
    carOrderDeliverDetailApp = (item: quotationResponse) => {


        // console.log('item',item.driverUserId)

        this.$scope.checkDetailCarId = item.driverUserId;



    }



}

angular.module("tms")
    .controller("orderDeliverDetailController", ["$scope", "orderService", "$location", "$ngBootbox", "$state", "routeService",
        "carService", "$http", "commonService", "$anchorScroll", "areaService", "$timeout", "locationService","ossUploaderService", IOrderDeliverManagentController_detail]);